﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;


namespace ChinaDynastyTours.Views
{
    public partial class LogForm : DevExpress.XtraEditors.XtraForm
    {
        public LogForm()
        {
            InitializeComponent();

            tree.Nodes[0].Tag = LogTypeEnum.全部;
            tree.Nodes[0].Nodes[0].Tag = LogTypeEnum.操作类;
            tree.Nodes[0].Nodes[1].Tag = LogTypeEnum.计调类;
            tree.Nodes[0].Nodes[2].Tag = LogTypeEnum.火车飞机;
            tree.Nodes[0].Nodes[3].Tag = LogTypeEnum.组团人;
            tree.Nodes[0].Nodes[4].Tag = LogTypeEnum.导游类;
            tree.Nodes[0].Expand(); 
            cblogtype.SelectedIndex = 0;
        }

        private void SyslogWindow_Load(object sender, EventArgs e)
        {
            this.tree.AfterSelect += new TreeViewEventHandler(OnSelectClick);
            this.btnQuery.Click += new EventHandler(OnbtnQueryClick);
        }

        private void OnSelectClick(object sender, EventArgs e)
        {
            if (tree.SelectedNode.Tag == null) return;

            LogTypeEnum type = (LogTypeEnum)tree.SelectedNode.Tag;
            logGridControl.DataSource = AddGridSelectedColumn(LogCtrl.GetLog(type));

        }

        private void OnbtnQueryClick(object sender, EventArgs e)
        {
            if (DateTime.Parse(dateTimeY.Text) <= DateTime.Parse(dateTimeM.Text))
            {
                try
                {
                    LogTypeEnum type;
                    switch (cblogtype.SelectedIndex)
                    {
                        case 0:
                            type = LogTypeEnum.全部;
                            break;
                        case 1:
                            type = LogTypeEnum.操作类;
                            break;
                        case 2:
                            type = LogTypeEnum.计调类;
                            break;
                        case 3:
                            type = LogTypeEnum.火车飞机;
                            break;
                        case 4:
                            type = LogTypeEnum.组团人;
                            break;
                        default:
                            type = LogTypeEnum.导游类;
                            break;
                    }
                    string startTime = (DateTime.Parse(dateTimeY.Text)).ToString("yyyy-MM-dd");
                    string endTime = dateTimeM.Checked == true ? (DateTime.Parse(dateTimeM.Text)).ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
                    logGridControl.DataSource = AddGridSelectedColumn(LogCtrl.GetLog(type, startTime, endTime));
                }
                catch { }

            }
            else
            {
                MessageBox.Show("开始时间必须小于结束时间", "信息提示");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (logGridView.FocusedRowHandle < 0) return;

            if (DialogResult.Yes== MessageBox.Show("确定删除当前选择的记录吗？","删除确认",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
            {
                int[] rowIds;
                logGridView.ClearSelection();
                rowIds = MutilRow(logGridView);
                for (int i = 0; i < rowIds.Length; i++)
                {
                    LogCtrl.DeleteLog(logGridView.GetRowCellValue(rowIds[i], "LogID").ToString());
                    logGridView.DeleteRow(rowIds[i]);
                }
            } 
        }

        private void btnDeleteClass_Click(object sender, EventArgs e)
        {
            if (tree.SelectedNode.Tag == null|| tree.SelectedNode.Equals(tree.Nodes[0])) return;

            string text = string.Format("确定清除‘{0}’分类记录吗？", tree.SelectedNode.Text);
            if (DialogResult.Yes == MessageBox.Show(text, "记录清理", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                LogCtrl.DeleteClassLog(tree.SelectedNode.Tag.ToString());
                OnSelectClick(null, null);
            }
        }

        #region 多选处理
        /// <summary>
        /// 添加‘选择’附加列
        /// </summary>
        /// <param name="dtSource"></param>
        /// <returns></returns>
        private DataTable AddGridSelectedColumn(DataTable dtSource)
        {
            dtSource.Columns.Add("Selected", typeof(bool));
            return dtSource;
        }

        /// <summary>
        /// 选择框多选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectedCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            #region 打钩选择行

            int iFocuse = logGridView.FocusedRowHandle;
            logGridView.ClearSelection();

            if ((sender as CheckEdit).Checked)
                logGridView.SelectRow(iFocuse);

            int[] rowIds = MutilRow(logGridView);

            #endregion
        }

        private int[] MutilRow(GridView gv)
        {
            object selected;
            for (int i = 0; i < gv.RowCount; i++)
            {
                selected = gv.GetRowCellValue(i, "Selected");

                if (string.IsNullOrEmpty(selected.ToString())) continue;

                if (Convert.ToBoolean(selected) == true)
                {
                    gv.SelectRow(i);
                }
            }
            return gv.GetSelectedRows();
        }

        private void tbSelectedAll_CheckedChanged(object sender, EventArgs e)
        {
            DataTable dt = logGridControl.DataSource as DataTable;
            foreach (DataRow dr in dt.Rows)
                dr["Selected"] = tbSelectedAll.Checked;
        } 
        #endregion
    }
}